define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/store/index',
                    edit_url: 'merchant/store/edit',
                    multi_url: 'merchant/store/multi',
                    table: 'store',
                }
            });

            var table = $("#table");

            table.on('click', '.business_license', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/business_license?id=' + id, '操作', {
                    area: ['600px', '450px']
                });
            });
            table.on('click', '.corporation', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/corporation?id=' + id, '操作', {
                    area: ['600px', '450px']
                });
            });
            table.on('click', '.bank', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/bank?id=' + id, '操作', {
                    area: ['600px', '450px']
                });
            });
            table.on('click', '.examine', function(re) {
                let id = $(this).data('id');
                Fast.api.open('merchant/store/examine/ids/' + id, '操作');
            });
            

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'uid', title: '用户ID'},
                        {field: 'user_phone|user_phone', title: '用户手机号', formatter: function(value, row, index) {
                            return row.user ? row.user.phone : '';
                        }},
                        {field: 'type', title: '店铺类型', searchList: window.typeOptions, formatter: function(value, row, index) {
                            return row.type_text;
                        }},
                        {field: 'photo', title: '门头照', events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'name', title: '店铺名称', operate: 'LIKE'},
                        {field: 'address', title: '店铺地址', operate: 'LIKE'},
                        {field: 'description', title: '简介', operate: 'LIKE'},
                        {field: 'is_invoice', title: '支持开发票', searchList: window.invoiceOptions, formatter: function(value, row, index) {
                            return window.invoiceOptions[value];
                        }},
                        {field: 'sub_mchid', title: '子商户号', operate: 'LIKE'},
                        {field: 'status', title: '店铺状态', searchList: window.statusOptions, formatter: function(value, row, index) {
                            return window.statusOptions[value];  
                        }},
                        {field: 'examine', title: '审核状态', searchList: window.examineOptions, formatter: function(value, row, index) {
                            return window.examineOptions[value];  
                        }},
                        {field: 'back_url', title: '扫脸url', searchable: false},
                        {field: '', title: '营业执照', searchable: false,formatter: function(value, data, index) {
                            if(data.business_license) {
                                return '<a href="javascript:" class="business_license" data-id="' + data.id + '">查看</a>';
                            }
                            return '';
                        }},
                        {field: '', title: '法人信息', searchable: false,formatter: function(value, data, index) {
                            if(data.corporation) {
                                return '<a href="javascript:" class="corporation" data-id="' + data.id + '">查看</a>';
                            }
                            return '';
                        }},
                        {field: '', title: '绑定银行卡信息', searchable: false,formatter: function(value, data, index) {
                            if(data.store_bank) {
                                return '<a href="javascript:" class="bank" data-id="' + data.id + '">查看</a>';
                            }
                            return '';
                        }},
                        {field: '', title: '审核', searchable: false,formatter: function(value, data, index) {
                            return '<a href="javascript:" class="examine" data-id="' + data.id + '">审核</a>';
                        }},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '更新时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        examine: function () {
            var examine = $('#examineSelect').val();
            var backUrlPanel = $('#backUrlPanel');
            var subMchidPanel = $('#subMchidPanel');
            var remarkPanel = $('#remarkPanel');

            if(examine == window.facescan) {
                backUrlPanel.show();
            } else {
                backUrlPanel.remove().show();
            }
            if(examine == window.complete) {
                subMchidPanel.show();
            } else {
                subMchidPanel.remove().show();
            }
            if(examine == window.refuse) {
                remarkPanel.show();
            } else {
                remarkPanel.remove().show();
            }
            $('#edit-form').on('change', '#examineSelect', function() {
                if($(this).val() == window.facescan) {
                    backUrlPanel.insertBefore('#buttonPanel');
                } else {
                    backUrlPanel.remove().find('input').val('');
                }
                if($(this).val() == window.complete) {
                    subMchidPanel.insertBefore('#buttonPanel');
                } else {
                    subMchidPanel.remove().find('input').val('');
                }
                if($(this).val() == window.refuse) {
                    remarkPanel.insertBefore('#buttonPanel');
                } else {
                    remarkPanel.remove().find('textarea').val('');
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
